package com.fractalist.base.core.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.fractalist.base.core.constants.SystemConstants;
import com.fractalist.base.core.web.BaseActionSupport;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;


public class IframeInterceptor implements Interceptor {

	private static final long serialVersionUID = -4143486594095738252L;

	public void destroy() {
	}

	public void init() {
	}

	public String intercept(ActionInvocation ai) throws Exception {
		HttpServletRequest req = ServletActionContext.getRequest();
		HttpSession session = req.getSession();
		Object obj = (Object) session
				.getAttribute(SystemConstants.USER_SESSION_KEY);
		if (obj == null) {
//			StringBuffer sb = new StringBuffer();
//			sb
//					.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Tranitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
//			sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
//			sb
//					.append("<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
//			sb.append("<title>跳转</title></head><body>");
//			sb
//					.append("<script type='text/javascript'>parent.window.location.href='");
//			sb.append(SystemConstants.SESSION_OUT_HREF + "';");
//			sb.append("</script>");
//			sb.append("</body></html>");
			ServletActionContext.getResponse().sendRedirect("/redirect.jsp");
//			.getWriter().write(sb.toString());
			return null;
		} else {
			Object obj1 = ai.getProxy().getAction();
			if (obj1 instanceof BaseActionSupport) {
				BaseActionSupport bas = (BaseActionSupport) obj1;
				bas.setMethodName(ai.getProxy().getMethod());
			}
			return ai.invoke();
		}
	}
}
